//
//  RecordProgress.m
//  NewChat
//
//  Created by songKX on 16/3/3.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "RecordProgress.h"

@implementation RecordProgress

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        
        self.backgroundColor = [UIColor clearColor];
        
        self.layer.cornerRadius = 5;
        
        self.clipsToBounds = YES;
        
    }
    return self;
    
}

-(void)setCenterText:(NSString *)string withAttributes:(NSDictionary *)attributes{
    
    CGFloat xCenter = self.frame.size.width * 0.5;
    
    CGFloat yCenter = self.frame.size.height * 0.5;
    
    CGSize strSize = [string sizeWithAttributes:attributes];
    CGFloat strX = xCenter - strSize.width * 0.5;
    CGFloat strY = yCenter - strSize.height * 0.5;
    [string drawAtPoint:CGPointMake(strX, strY) withAttributes:attributes];
    
}

//重写set方法

- (void)setProgress:(CGFloat)progress
{
    _progress = progress;
    
    dispatch_async(dispatch_get_main_queue(), ^{
       
        if(progress >=15){
            
            self.progress = 15;
            
            
        }else{
        
        
            [self setNeedsDisplay];
        }
        
    });
    
}



+(instancetype)progressView{
    
    
    return [[self alloc] init];
}

-(void)drawRect:(CGRect)rect{
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGFloat xCenter = rect.size.width * 0.5;
    
    CGFloat yCenter = rect.size.height * 0.5;
    CGFloat radius = MIN(rect.size.width * 0.5, rect.size.height * 0.5)- 2;
    
    
    // 进度环边框
    [[UIColor grayColor] set];
    CGFloat w = radius * 2 + 1;
    CGFloat h = w;
    CGFloat x = (rect.size.width - w) * 0.5;
    CGFloat y = (rect.size.height - h) * 0.5;
    CGContextAddEllipseInRect(ctx, CGRectMake(x, y, w, h));
    CGContextStrokePath(ctx);

    //进度环
    [[UIColor blueColor] set];
    CGContextMoveToPoint(ctx, xCenter, yCenter);
    CGContextAddLineToPoint(ctx, xCenter, 0);
    CGFloat to = - M_PI * 0.5 + self.progress/15 * M_PI * 2 + 0.001; // 初始值
    CGContextAddArc(ctx, xCenter, yCenter, radius, - M_PI * 0.5, to, 0);
    CGContextClosePath(ctx);
    CGContextFillPath(ctx);
    
    // 遮罩
    [[UIColor cyanColor] set];
    CGFloat maskW = (radius - 15) * 2;
    CGFloat maskH = maskW;
    CGFloat maskX = (rect.size.width - maskW) * 0.5;
    CGFloat maskY = (rect.size.height - maskH) * 0.5;
    CGContextAddEllipseInRect(ctx, CGRectMake(maskX, maskY, maskW, maskH));
    CGContextFillPath(ctx);
    
//    // 遮罩边框
    [[UIColor clearColor] set];
    CGFloat borderW = maskW + 1;
    CGFloat borderH = borderW;
    CGFloat borderX = (rect.size.width - borderW) * 0.5;
    CGFloat borderY = (rect.size.height - borderH) * 0.5;
    CGContextAddEllipseInRect(ctx, CGRectMake(borderX, borderY, borderW, borderH));
    CGContextStrokePath(ctx);
    
    // 进度数字
    NSString *progressStr = [NSString stringWithFormat:@"%.0f%s", self.progress, "\""];
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSFontAttributeName] = [UIFont boldSystemFontOfSize:self.frame.size.width*0.3];
    attributes[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    [self setCenterText:progressStr withAttributes:attributes];
    
    
}


@end
